package com.psyhotline.utils;

import com.alibaba.fastjson.JSONObject;
import com.psyhotline.constant.Constant;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Cookie {
    public static String getCookieLogin_admin() throws IOException {
        JSONObject account = new JSONObject();
        account.put("username", "admin");
        account.put("password", "173afad5992a3f73a472fc09b05b1fb7");

        JSONObject json = account;
        String outPath = Constant.getBaseUrl() + "/auth/login";

        URL newURL = new URL(outPath);
        HttpURLConnection conn = (HttpURLConnection) newURL.openConnection();
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
        conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
        out.write(String.valueOf(json));
        out.flush();
        out.close();
        InputStream inputStream = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        reader.close();
        String headerName = null;
        StringBuilder myCookies = new StringBuilder();
        for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
            if (headerName.equals("Set-Cookie")) {
                String cookie = conn.getHeaderField(i);
                cookie = cookie.substring(0, cookie.indexOf(";"));
                String cookieName = cookie.substring(0, cookie.indexOf("="));
                String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
                myCookies.append(cookieName + "=");
                myCookies.append(cookieValue);
            }
        }
        String cookie = myCookies.toString();
        return cookie;
    }

    public static String getCookieLogin_counselor() throws IOException {
        JSONObject account = new JSONObject();
        account.put("username", "test4");
        account.put("password", "e10adc3949ba59abbe56e057f20f883e");

        JSONObject json = account;
        String outPath = Constant.getBaseUrl() + "/auth/login";

        URL newURL = new URL(outPath);
        HttpURLConnection conn = (HttpURLConnection) newURL.openConnection();
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
        conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
        out.write(String.valueOf(json));
        out.flush();
        out.close();
        InputStream inputStream = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        reader.close();
        String headerName = null;
        StringBuilder myCookies = new StringBuilder();
        for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
            if (headerName.equals("Set-Cookie")) {
                String cookie = conn.getHeaderField(i);
                cookie = cookie.substring(0, cookie.indexOf(";"));
                String cookieName = cookie.substring(0, cookie.indexOf("="));
                String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
                myCookies.append(cookieName + "=");
                myCookies.append(cookieValue);
            }
        }
        String cookie = myCookies.toString();
        return cookie;
    }

    public static String getCookieLogin_supervisor() throws IOException {
        JSONObject account = new JSONObject();
        account.put("username", "test1");
        account.put("password", "e10adc3949ba59abbe56e057f20f883e");

        JSONObject json = account;
        String outPath = Constant.getBaseUrl() + "/auth/login";

        URL newURL = new URL(outPath);
        HttpURLConnection conn = (HttpURLConnection) newURL.openConnection();
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
        conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
        out.write(String.valueOf(json));
        out.flush();
        out.close();
        InputStream inputStream = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        reader.close();
        String headerName = null;
        StringBuilder myCookies = new StringBuilder();
        for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
            if (headerName.equals("Set-Cookie")) {
                String cookie = conn.getHeaderField(i);
                cookie = cookie.substring(0, cookie.indexOf(";"));
                String cookieName = cookie.substring(0, cookie.indexOf("="));
                String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
                myCookies.append(cookieName + "=");
                myCookies.append(cookieValue);
            }
        }
        String cookie = myCookies.toString();
        return cookie;
    }

    public static String getCookieLogin_user() throws IOException {
        JSONObject account = new JSONObject();
        account.put("JSESSIONID", "61DEFBFEB2BA96977A1F515D8E7AFB05");

        JSONObject json = account;
        String outPath = Constant.getBaseUrl() + "/auth/login";

        URL newURL = new URL(outPath);
        HttpURLConnection conn = (HttpURLConnection) newURL.openConnection();
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
        conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
        out.write(String.valueOf(json));
        out.flush();
        out.close();
        InputStream inputStream = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        reader.close();
        String headerName = null;
        StringBuilder myCookies = new StringBuilder();
        for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
            if (headerName.equals("Set-Cookie")) {
                String cookie = conn.getHeaderField(i);
                cookie = cookie.substring(0, cookie.indexOf(";"));
                String cookieName = cookie.substring(0, cookie.indexOf("="));
                String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
                myCookies.append(cookieName + "=");
                myCookies.append(cookieValue);
            }
        }
        String cookie = myCookies.toString();
        return cookie;
    }
}

